package com.jk.controller;

import com.jk.pojo.UserBean;
import com.jk.utils.AlipayUtils;
import com.jk.utils.AliyunUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;

/**
 * @ClassName AlipayController
 * @Descripttion TODO
 * @Author 支付宝充值
 * @Date 2022/10/20 19:19
 * @Version 0.001
 */
@RestController
public class AlipayController {

    @Autowired
    private AlipayUtils aliPayUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    //支付方法
    @RequestMapping("aliPay")
    public String aliPay(String total_amount, String subject, String body, HttpServletResponse httpResponse, Integer userid, String orderid, String out_trade_no, HttpSession session) throws IOException {


        //取登录的信息
//        UserBean user = (UserBean) session.getAttribute("user");

        UserBean user = (UserBean) redisTemplate.opsForValue().get("denglu");



        //付款金额，必填（动态的就选择动态获取）

        //订单名称，必填
        subject = "支付宝支付";


        //订单号
        out_trade_no = getMsgCode();
        //商品描述，可空
        body = "尊敬的"+user.getUser_name()+"您好，您的充值金额是"+total_amount;
        System.out.println(total_amount+"======"+subject+"======"+out_trade_no+"====="+body);
        aliPayUtil.alipay(total_amount,subject,body,httpResponse,out_trade_no,userid);
        return "购买成功！！！";
    }

    /**
    * @Author: binzi
    * @Description:
    * @Date: 2022/11/8 10:45
    * @Return: java.lang.String
    **/
    private static String getMsgCode() {
        int n = 6;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        return code.toString();
    }
}
